package implementor;

import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;

import java.util.Random;

public class TextureFilter implements Filter {
    private final String textureType;
    
    public TextureFilter() {
        this("noise");
    }
    
    public TextureFilter(String textureType) {
        this.textureType = textureType;
    }
    
    @Override
    public Mat apply(Mat image) {
        Mat result = image.clone();
        
        switch (textureType.toLowerCase()) {
            case "noise":
                addNoise(result);
                break;
            case "grain":
                addGrain(result);
                break;
            default:
                addNoise(result);
        }
        
        return result;
    }
    
    private void addNoise(Mat image) {
        Mat noise = new Mat(image.size(), CvType.CV_8UC3);
        Random random = new Random();
        
        // 生成随机噪声
        Core.randn(noise, 0, 50);
        
        // 添加到原图
        Core.add(image, noise, image);
    }
    
    private void addGrain(Mat image) {
        // 增加对比度
        image.convertTo(image, -1, 1.2, 0);
        
        // 添加颗粒感
        Mat noise = new Mat(image.size(), CvType.CV_8UC3);
        Core.randn(noise, 0, 20);
        Core.add(image, noise, image);
    }
    
    @Override
    public String getName() {
        return "Texture(" + textureType + ")";
    }
}